package com.example.administrator.waitan.tools;

import android.graphics.Bitmap;
import android.util.LruCache;

import com.android.volley.toolbox.ImageLoader;

/**
 * Created by Risky on 15/10/20.
 */
public class MemoryCache implements ImageLoader.ImageCache {

    private LruCache<String,Bitmap> lruCache;//创建内存对象

    public MemoryCache() {
        long maxMemory = Runtime.getRuntime().maxMemory();//得到运行时最大内存
        int cacheSize = (int) (maxMemory / 4);
        lruCache = new LruCache<String,Bitmap>(cacheSize){

            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes()*value.getHeight()/1024;
            }
        };//构建内存对象 并设置内存大小

    }

    @Override
    public Bitmap getBitmap(String url) {
        return lruCache.get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        lruCache.put(url,bitmap);
    }
}
